package com.flmes.base.base.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author: flmes
 * @Date: 2024/9/27 19:23
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    /**
     * 返回码,200为成功,见{@link ResultCode 返回码}
     */
   private Integer code;

    /**
     * 返回码说明
     */
   private String msg;

    /**
     * 数据
     */
   private T data;

    /**
     * 服务器当前时间
     */
   private Long nowTime;

    /**
     * 成功返回
     * @return
     * @param <T>
     */
    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), null, System.currentTimeMillis());
    }

    /**
     * 成功返回
     * @param data
     * @return
     * @param <T>
     */
   public static <T> Result<T> success(T data) {
       return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), data, System.currentTimeMillis());
   }

    /**
     * 失败返回
     * @param code
     * @param messages
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail(ResultCode code, Object... messages){
        return new Result(code.getCode(),
                messages == null || messages.length == 0 ? code.getDesc() : String.format(code.getDesc(), messages),
                null,
                System.currentTimeMillis());
    }

}
